package com.zp.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class CountLineListener implements HttpSessionListener {


    @Override
    public void sessionCreated(HttpSessionEvent event) {
        System.out.println("监听器创建了");
        ServletContext context = event.getSession().getServletContext();
        Integer count = (Integer)context.getAttribute("count");
        if (count == null) {
            count = new Integer(1);
        } else {
            int cnt = count.intValue();
            count = new Integer(cnt + 1);
        }
        System.out.println(count);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        System.out.println("监听器销毁了");
        ServletContext context = event.getSession().getServletContext();
        Integer count = (Integer) context.getAttribute("count");
        int cnt = count.intValue();
        count = new Integer(cnt - 1 < 0 ? 0:cnt-1);
        context.setAttribute("count", count);
    }
}
